﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DienThoai
{
    /// <summary>
    /// Interaction logic for UpdateStaff.xaml
    /// </summary>
    public partial class UpdateStaff : Window
    {
        private Staff staff;
        DienThoaiDataContext db = new DienThoaiDataContext();
        public UpdateStaff(Staff staff)
        {
            InitializeComponent();
            this.staff = staff;

            var Roles = from i in db.Roles
                        where i.RoleID != "1"
                        select i.RoleName;
            foreach (var role in Roles)
                cbbRole.Items.Add(role);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtUsername.Text = staff.Username;
            txtPassword.Text = staff.Password;
            cbbRole.SelectedValue = staff.Role.RoleName;
            txtFullName.Text = staff.Person.Fullname;
            dpDOB.SelectedDate = staff.Person.DOB;
            txtAddress.Text = staff.Person.Address;
            txtPhone.Text = staff.Person.Phone;

        }

        private void btCreateNew_Click(object sender, RoutedEventArgs e)
        {
            float number;
            bool isExisted = false;
            var StaffList = from i in db.Staffs
                            where staff.PersonID != i.PersonID
                            select i.Username;

            if (txtUsername.Text == "")
                MessageBox.Show("Chưa nhập UserName.");
            else if (txtPassword.Text == "")
                MessageBox.Show("Chưa nhập Password.");
            else if (txtFullName.Text == "")
                MessageBox.Show("Chưa nhập họ tên đầy đủ");
            else if (dpDOB.SelectedDate == null)
                MessageBox.Show("Chưa chọn ngày sinh");
            else if (txtAddress.Text == "")
                MessageBox.Show("Chưa nhập địa chỉ");
            else if (txtPhone.Text != "" && float.TryParse(txtPhone.Text, out number) == false)
                MessageBox.Show("Số điện thoại có chứa ký tự.");
            else
            {
                foreach (var existStaff in StaffList)
                    if (txtUsername.Text == existStaff)
                        isExisted = true;
                if (isExisted)
                    MessageBox.Show("Username đã tồn tại trong CSDL. Xin chọn UserName khác.");
                else
                {
                    var updateStaff = from i in db.Staffs
                                      where i.PersonID == staff.PersonID
                                      select i;
                    var newRole = from i in db.Roles
                                  where i.RoleName == cbbRole.SelectedValue.ToString()
                                  select i.RoleID;
                    foreach (var i in updateStaff)
                    {
                        i.Username = txtUsername.Text;
                        i.Password = txtPassword.Text;
                        i.Person.Fullname = txtFullName.Text;
                        i.Person.DOB = dpDOB.SelectedDate;
                        i.Person.Address = txtAddress.Text;
                        i.Person.Phone = txtPhone.Text;
                        foreach (var k in newRole)
                            i.RoleID = k;
                        db.SubmitChanges();
                    }
                    MessageBox.Show("Sửa đổi nhân viên " + txtFullName.Text + " thành công.");
                    DialogResult = true;
                }
            }
        }

        private void btCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
